package com.gwd.task.controller;

import com.gwd.task.group.GroupInsert;
import com.gwd.task.group.GroupUpdate;
import com.gwd.task.model.PageUtils;
import com.gwd.task.model.ProjectDTO;
import com.gwd.task.model.ProjectModuleDTO;
import com.gwd.task.services.ProjectModuleServer;
import com.gwd.task.services.ProjectServer;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhiyongming
 * @date 2020/11/11 10:06
 */
@RestController
@RequestMapping(value = "/v1/project")
public class ProjectController {

    @Resource
    private ProjectServer projectServer;
    @Resource
    private ProjectModuleServer projectModuleServer;

    /**
     * 按组查询
     * @return List<ProjectDTO>
     */
    @GetMapping(value = "/findByGroup")
    public List<ProjectDTO> findGroup(@RequestParam(value = "groupId") Long groupId) {
        return projectServer.findByGroup(groupId);
    }

    @GetMapping(value = "/getModules")
    public List<ProjectModuleDTO> getModulesByProject(@RequestParam(value = "projectId") Long projectId) {
        return projectModuleServer.findByProject(projectId);
    }

    /**
     * 按分页查询
     * @param dto dto
     * @param pageUtils 分页内容
     * @return 分页内容
     */
    @GetMapping(value = "/findProjectByPage")
    public PageUtils findGroupByPage(ProjectDTO dto, @Validated PageUtils pageUtils) {
        return projectServer.findByPage(dto, pageUtils);
    }

    @GetMapping(value = "/findAll")
    public List<ProjectDTO> findAll() {
        return projectServer.findAll();
    }

    /**
     * 新增
     * @param dto dto
     */
    @PostMapping(value = "/add")
    public void add(@RequestBody @Validated({GroupInsert.class}) ProjectDTO dto) {
        projectServer.save(dto, dto.getModules());
    }

    /**
     * 修改
     * @param dto dto
     */
    @PutMapping(value = "/edit")
    public void edit(@RequestBody @Validated({GroupUpdate.class}) ProjectDTO dto) {
        projectServer.save(dto, dto.getModules());
    }

    @PostMapping(value = "/editModules")
    public void editModules(@RequestBody ProjectDTO dto) {
        projectServer.saveModules(dto.getProjectId(), dto.getGroupId(), dto.getModules());
    }

    /**
     * 删除
     * @param id 项目id
     */
    @DeleteMapping(value = "/remove/{id}")
    public void remove(@PathVariable("id") Long id) {
        projectServer.remove(id);
    }

    /**
     * 状态切换
     * @param id 项目id
     */
    @PutMapping(value = "/active/{id}")
    public void active(@PathVariable("id") Long id) {
        projectServer.active(id);
    }

}
